package com.taomu.app.zongchouhuli.moudle.mine.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.framework.BaseActivity;
import com.taomu.app.zongchouhuli.http.res.ResUserInfoEntity;
import com.taomu.app.zongchouhuli.moudle.mine.contract.ConfirmRechargeContract;
import com.taomu.app.zongchouhuli.moudle.mine.contract.impl.ConfirmRechargePresenter;
import com.taomu.app.zongchouhuli.utils.ParseUtil;
import com.taomu.app.zongchouhuli.utils.StorageHelper;
import com.taomu.app.zongchouhuli.weight.popwindow.PopSelectPayViewManager;

public class UserConfirmRechargeActivity extends BaseActivity<ConfirmRechargeContract.Presenter> implements ConfirmRechargeContract.View{

    private ImageView mBackIm;

    private TextView mOrderPriceTv;

    private EditText mRechargeAccountEt;

    private EditText mPayAccountEd;

    private TextView mPayWayTv;

    private ImageView mChangedPayWayIm;

    private EditText mTimeEt;

    private EditText mPayPriceEt;

    private EditText mContractEt;

    private TextView mSubmitTv;

    private TextView mOrderNumTv;

    private PopSelectPayViewManager popManager;

    public static final String TOTLEFEE = "totelfee";

    public static final String ORDERNUM = "ordernum";

    public static final String PORDUCTID = "productid";

    public static final String ORDERID = "orderid";

    public final static void start(Context mContext, String productid, String orderid,String totlefee, String orderNum){
        Intent intent = new Intent(mContext,UserConfirmRechargeActivity.class);
        intent.putExtra(TOTLEFEE, totlefee);
        intent.putExtra(ORDERNUM, orderNum);
        intent.putExtra(PORDUCTID, productid);
        intent.putExtra(ORDERID, orderid);
        mContext.startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        mPresenter.onDetch();
        super.onDestroy();
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_confirm_recharge;
    }

    @Override
    protected void initView() {
        popManager = new PopSelectPayViewManager(this);
        mBackIm = findViewById(R.id.recharge_confirm_back_im);
        mOrderPriceTv = findViewById(R.id.recharge_confirm_price_tv);
        mRechargeAccountEt = findViewById(R.id.recharge_confirm_acccount_et);
        mPayAccountEd = findViewById(R.id.recharge_confirm_pay_acccount_et);
        mOrderNumTv = findViewById(R.id.recharge_confirm_order_num);
        mPayWayTv = findViewById(R.id.recharge_confirm_pay_way_tv);
        mChangedPayWayIm = findViewById(R.id.recharge_confirm_pay_way_im);
        mTimeEt = findViewById(R.id.recharge_confirm_time_et);
        mPayPriceEt = findViewById(R.id.recharge_confirm_price_et);
        mContractEt = findViewById(R.id.recharge_confirm_contract_et);
        mSubmitTv = findViewById(R.id.recharge_confirm_submit_tv);
    }

    private String productid;

    private String orderid;

    private String totolMoney;

    @Override
    protected void initData() {
        popManager.setPopSelectPayWayListener(new PopSelectPayViewManager.PopSelectPayWayListener() {
            @Override
            public void onItemClick(String type) {
                mPayWayTv.setText(type);
            }
        });
        //mPresenter.getConfirmCanClick(mRechargeAccountEt,mPayAccountEd,mPayAccountEd,mContractEt);
        if(getIntent() != null){
            String totlemoney = getIntent().getStringExtra(TOTLEFEE);
            String orderNum = getIntent().getStringExtra(ORDERNUM);
            productid = getIntent().getStringExtra(PORDUCTID);
            orderid = getIntent().getStringExtra(ORDERID);
            mOrderPriceTv.setText("¥ "+totlemoney);
            totolMoney = totlemoney;
            mOrderNumTv.setText("订单编号："+orderNum);
            mContractEt.setText(ParseUtil.parseObject(StorageHelper.getAccountInfo(), ResUserInfoEntity.class).getPhone());
            mRechargeAccountEt.setText(ParseUtil.parseObject(StorageHelper.getAccountInfo(), ResUserInfoEntity.class).getPhone());
            mPayAccountEd.setText(ParseUtil.parseObject(StorageHelper.getAccountInfo(), ResUserInfoEntity.class).getUsername());
            mPayPriceEt.setText(totolMoney);
            //            ParseUtil.parseObject(StorageHelper.getAccountInfo(), ResUserInfoEntity.class).getAccount();
        }
    }

    @Override
    protected void initEvent() {
        mBackIm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mChangedPayWayIm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popManager.showPopWindow(mPayWayTv);
            }
        });
        mSubmitTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到结果界面
                if(TextUtils.isEmpty(getRechargeAccount())){
                    showToast("请输入充值会员账号");
                    return;
                }
                if(TextUtils.isEmpty(getPayAccount())){
                    showToast("请输入支付账号或昵称");
                    return;
                }

                if(TextUtils.isEmpty(totleMoney())){
                    showToast("请输入充值金额");
                    return;
                }
                if(TextUtils.isEmpty(contract())){
                    showToast("请输入联系方式");
                    return;
                }
                mPresenter.submit(productid, orderid, getPayAccount(), getRechargeAccount(), getTime(), totolMoney, totleMoney(),contract(),getPayAccount(),getRechargeAccount());
            }
        });
    }

    @Override
    protected ConfirmRechargeContract.Presenter createPresenter() {
        return new ConfirmRechargePresenter(this);
    }

    @Override
    public void setConfirmCanClick(int bgcolor,int textcolor ,boolean enable) {
        mSubmitTv.setEnabled(enable);
        mSubmitTv.setBackgroundColor(getResources().getColor(bgcolor));
        mSubmitTv.setTextColor(getResources().getColor(textcolor));
    }

    @Override
    public void setSumitResults(String msg) {
        showToast(msg);
        Intent intent = new Intent(this,UserRechargeResultsActivity.class);
        intent.putExtra("isrecharege", true);
        startActivity(intent);
        finish();
    }

    @Override
    public void setPresenter(ConfirmRechargeContract.Presenter presenter) {

    }

    private String getRechargeAccount(){
        return mRechargeAccountEt.getText().toString();
    }

    private String getPayAccount(){
        return mPayAccountEd.getText().toString();
    }

    private String getTime(){
        return mTimeEt.getText().toString();
    }

    private String totleMoney(){
        return mPayPriceEt.getText().toString();
    }

    private String contract(){
        return mContractEt.getText().toString();
    }

    private String getOrderPrice(){
        return mOrderPriceTv.getText().toString();
    }

}
